package zombiedefense.framework;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 * This class provides a framework for a game that progresses
 * through several states throughout its lifetime.
 * @author &{user}
 */
public abstract class GameStateManager {
    
    protected GameState currentState;
    private boolean running = true;
    
    private long sleepTime = 5;
    private long previousRenderTime;
    private long elapsedTime;
    
    /**
     * This method should be the insertion point for the game thread.
     */
    public final void run(){
	previousRenderTime = System.currentTimeMillis();
	
	while(running){
	    
	    //advance to next state
	    currentState = getGameState();
	    
	    //update current state
	    update();
	    
	    //draw current state
	    draw();
	    
	    //sleep time
	    try{
		Thread.sleep(sleepTime);
	    }catch(Exception e){}
	}
    }
    
    public final void update(){
	//measure the amount of time inbetween game updates and update the game accordingly
	elapsedTime = System.currentTimeMillis() - previousRenderTime;
	previousRenderTime = System.currentTimeMillis();
	    
	//update game state
	currentState.update(elapsedTime);
    }
    
    public abstract GameState getGameState();
    
    /*
     * Will be overridden to draw game state to screen
     */
    public abstract void draw();
    
    public void stop(){
	running = false;
    }
    
}
